using Microsoft.AspNetCore.Mvc;
using MsgBackend.Api.ParamModel;
using MsgBackend.Api.Repository;
using MsgBackend.Api.Models;
using System;
using Microsoft.Extensions.Configuration;
using System.Linq;

namespace MsgBackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {

        private IRepository<Users> _usersRepository;

        private readonly IConfiguration _configuration;
        public UserController(IConfiguration configuration, IRepository<Users> usersRepository)
        {
            _usersRepository = usersRepository;
            _configuration = configuration;
        }

        //登录
        [HttpPost, Route("Login")]
        public ActionResult GetToken(ParamUser model)
        {

            var users = _usersRepository.Table;

            if (model.Username.Trim() == "" || model.Password.Trim() == "")
            {
                var res = new
                {
                    Code = 1002,
                    Data = "",
                    Msg = "账号密码不能为空，请重试"
                };
                return Ok(res);
            }
            else
            {
                var user = users.Where(x => x.Username == model.Username.Trim() && x.Password == model.Password.Trim()).FirstOrDefault();

                if (user == null)
                {
                    return Ok(
                    new
                    {
                        Code = 1002,
                        Data = "",
                        Msg = "账号或密码错误，请确认后重试"
                    });

                };

                //如果登录成功，需要获得token
                var res = new
                {
                    Code = 1000,
                    Data = user,
                    Msg = "登录成功"
                };
                return Ok(res);

            }
        }

        //获取所有用户     
        [HttpGet]
        public ActionResult Get()
        {
            var users = _usersRepository.Table;
            var use = users.Where(x => x.IsActived == true && x.IsDeleted == false);
            return Ok(new
            {
                Code = 1000,
                Data = use,
                Msg = "获取成功"
            });
        }


        //查找用户数据
        [HttpGet, Route("Select")]
        public dynamic SelectUser([FromQuery] SelectMsg data)
        {
            var msg = data.msg;

            var use = _usersRepository.Table;

            var u = use.Where(x => x.IsActived == true && x.IsDeleted == false).Where(x => x.Username.Contains(msg) || x.Remark.Contains(msg));

            int id = 0;
            try
            {

                id = int.Parse(msg);

            }
            catch
            {
                Console.WriteLine("无法转为int类型");
            }

            var u2 = _usersRepository.GetById(id);




            if (u2.IsActived == true && u2.IsDeleted == false)
            {
                return Ok(new
                {
                    Code = 1000,
                    Data = u,
                    Data2 = u2,
                });
            }
            else
            {
                return Ok(new
                {
                    Code = 1000,
                    Data = u,
                    Data2 = "",
                });
            }
        }


        //添加新的用户
        [HttpPost]
        public ActionResult Post(ParamUser model)
        {
            var user = new Users
            {
                Username = model.Username,
                Password = model.Password
            };
            _usersRepository.Insert(user);
            return Ok(new
            {
                Code = 1000,
                Data = user,
                Msg = "添加成功"
            });
        }

        //根据id修改指定用户
        [HttpPut("{id}")]
        public ActionResult Put(ParamUser model, int id)
        {
            var user = _usersRepository.GetById(id);
            if (user != null)
            {
                user.Username = model.Username;
                user.Password = model.Password;
                _usersRepository.Update(user);
                return Ok(new
                {
                    Code = 1000,
                    Data = user,
                    Msg = "修改成功"
                });
            }
            return Ok(new
            {
                Code = 1000,
                Data = "",
                Msg = "获取失败，请检查id是否正确"
            });

        }
        //根据用户名密码修改用户
        [HttpPut, Route("UserUpdate")]

        public ActionResult UserUpdate(ParamUpdateUser model)
        {
            var oldPassword = model.OldPassword;
            var Username = model.Username;
            var users = _usersRepository.Table;
            var user = users.Where(x => x.Username == Username && x.Password == oldPassword).FirstOrDefault();
            if (user != null)
            {
                user.Password = model.NewPassword;
                _usersRepository.Update(user);
                return Ok(new
                {
                    Code = 1000,
                    Data = user,
                    Msg = "修改成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = "",
                Msg = "修改失败"
            });

        }

        //删除用户数据
        [HttpDelete("{id}")]
        public ActionResult DeleteUser(int id)
        {
            var use = _usersRepository.GetById(id);
            if (use != null)
            {

                use.IsActived = false;
                use.IsDeleted = true;
                use.UpdatedTime = DateTime.Now;

                _usersRepository.Delete(use);

                return Ok(new
                {
                    Code = 1000,
                    Data = use,
                    Msg = string.Format("删除指定id为{0}的用户成功", id)
                });
            }
            else
            {
                return Ok(new
                {
                    Code = 1002,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                });
            }
        }
    }
}